﻿#include "GUISpectraEditNotesDialog.h"
#include "ui_GUISpectraEditNotesDialog.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/CentralWidget.h"
#include "GUIWidget/BrowserWidget.h"

#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"

namespace GUI
{
    GUISpectraEditNotesDialog::GUISpectraEditNotesDialog(int spectraID) :
        GUIDialogBase(FITKAPP->getGlobalData()->getMainWindow()), _spectraID(spectraID)
    {
        _ui = new Ui::GUISpectraEditNotesDialog();
        _ui->setupUi(this);

        if (_physics == nullptr)return;
        Acoustics::FITKAcousticsSpectraManager* spectraManager = _physics->getFITKAcousticsSpectraManager();
        if (spectraManager == nullptr)return;
        Acoustics::FITKAcousticsAbstractSpectra* spectarData = spectraManager->getDataByID(_spectraID);
        if (spectarData == nullptr)return;
        QString nodes = spectarData->getNotes();
        _ui->textEdit->setText(nodes);

        setWindowTitle(tr("Edit Notes"));
    }

    GUISpectraEditNotesDialog::~GUISpectraEditNotesDialog()
    {
        if (_ui) {
            delete _ui;
            _ui = nullptr;
        }
    }

    void GUISpectraEditNotesDialog::on_pushButton_OK_clicked()
    {
        if (_physics == nullptr)return;
        Acoustics::FITKAcousticsSpectraManager* spectraManager = _physics->getFITKAcousticsSpectraManager();
        if (spectraManager == nullptr)return;
        Acoustics::FITKAcousticsAbstractSpectra* spectarData = spectraManager->getDataByID(_spectraID);
        if (spectarData == nullptr)return;
        QString nodes = _ui->textEdit->toPlainText();
        spectarData->setNotes(nodes);

        //从数据结构中获取表单对象
        GUI::MainWindow* mw = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mw && mw->getCentralWidget() && mw->getCentralWidget()->getGraphArea()) {
            GUI::BrowserWidget* bw = mw->getCentralWidget()->getGraphArea()->getCurrentWindow<GUI::BrowserWidget>();
            if (bw) {
                bw->updateTable();
            }
        }

        this->accept();
    }

    void GUISpectraEditNotesDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }
}